package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import org.apache.ibatis.annotations.Param;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fengkai 去除多余空格
 * 输入为两行字符串:
 * 第一行: 待去除多余空格的文本，用例保证如果有单引号，则单引号成对出现，且单引号可能有多对.
 * 第二行: 关键词的开始和结束坐标，关键词间以逗号区分，关键词内的开始和结束位置以单空格区分
 * @creat 2023/4/1
 */
public class RemoveOverSpace {
    public static String myMethod(String strLine, String keyLine){
        String[] s = keyLine.split(",");
        ArrayList<String> keyWords = new ArrayList<>();
        for (String i: s) {
            String[] index = i.split(" ");
            keyWords.add(strLine.substring(Integer.parseInt(index[0]), Integer.parseInt((index[1]))+1));
        }
        strLine = makeString(strLine);
        System.out.println(strLine);
        String res = "";
        for (String keyWord : keyWords) {
            System.out.println(keyWord);
            int i = strLine.indexOf(keyWord);
            res += "[" + i + "," + (i + keyWord.length() - 1) + "],";
        }
        System.out.println(res.substring(0, res.length() - 1));
        return null;
    }

    private static String makeString (String strLine) {
        StringBuffer stringBuffer = new StringBuffer("");
        String[] s = strLine.split(" ");
        for (String s1 :s){
            if (!s1.equals("")){
                stringBuffer.append(s1 + " ");
            }
        }
        Pattern compile = Pattern.compile("\\'(.*?)\\'+");
        Matcher matcher = compile.matcher(strLine);
        //存储单引号信息
        ArrayList<String> strings = new ArrayList<>();
        while (matcher.find()) {
            strings.add(matcher.group());
        }

        int index[] =new int[strings.size() * 2];
        for (int i = 0; i < strings.size() * 2; i++) {
            if (i == 0){
                index[i] = stringBuffer.indexOf("'");
            }else {
                index[i] = stringBuffer.indexOf("'", index[i - 1] + 1);
            }
        }
        ArrayList<String> subStrings = new ArrayList<>();
        ArrayList<Integer> indexList = new ArrayList<>();
        indexList.add(0);
        for (int v: index) {
            indexList.add(v);
        }
        indexList.add(stringBuffer.length() - 1);
        for (int i = 0; i < indexList.size() / 2; i++) {
            if ( i == 0 && indexList.get(2*i + 1) == 0){
                subStrings.add(" ");
            }
            if (i != 0){
                subStrings.add(stringBuffer.substring(indexList.get(2*i) + 1, indexList.get(2*i + 1)));
            }else if (i ==  indexList.size() / 2 - 1 && indexList.get(2*i + 1) == indexList.get(2*i)){
                subStrings.add(" ");
            } else {
                subStrings.add(stringBuffer.substring(indexList.get(2*i), indexList.get(2*i + 1)));
            }
            if (i < strings.size()){
                subStrings.add(strings.get(i));
            }
        }
        stringBuffer = new StringBuffer("");
        for (String subString : subStrings) {
            if (!subString.equals("") && !subString.equals(" ")){
                stringBuffer.append(subString);
            }
        }
        return stringBuffer.toString();
    }

       public static void main(String[] args){
//           System.out.println(makeString("Life is painting a picture, not doing 'a sum'"));

           myMethod("Life    'i  s' painting 'a p' icture, not doing 'a sum'", "0 3");
       }
}
